/**
 * 
 */
package cn.java.innerclass;

/**
* 匿名内部类案例，实现课程排序
* @author 攀博课堂
* @version v1.0
*/
public class ICourseSortTest {


	
	public static void main(String[] args) {
		
		//准备课程列表
		PbCourse[] courses = new PbCourse[] {
				
				new PbCourse(1L, "java面向对象开发-基础篇", 20f),
				new PbCourse(2L, "java面向对象开发-高级篇", 30f),
				new PbCourse(3L, "javaWeb开发", 30f),
				new PbCourse(3L, "javaWeb开发", 30f),
				new PbCourse(4L, "javaWeb项目之攀博课堂公众号项目", 50f),
				new PbCourse(5L, "java分布式项目之攀博课堂在线教育平台", 50f),
				new PbCourse(6L, "java分布式项目之攀博课堂聚合支付平台", 50f)
		};
		
		//定义匿名内部类,针对ICourseSort接口定义匿名内部类
		ICourseSort iCourseSort = new ICourseSort() {
			
			//实现排序逻辑,按id降序排序
			@Override
			public void sort(PbCourse[] courses) {
				for (int i = 0; i < courses.length; i++) {
					for (int j = i+1; j < courses.length; j++) {
						if(courses[i]!=null
								&& courses[j]!=null
								&& courses[i].getId()<courses[j].getId()) {
							//元素交换
							PbCourse course = courses[i];
							courses[i] = courses[j];
							courses[j] = course;
						}
					}
				}
				
			}
		};
		
		//通过匿名内部类对象进行排序，调用sort方法
		iCourseSort.sort(courses);
		
		//遍历查询
		for (int i = 0; i < courses.length; i++) {
			System.out.println(courses[i]);
		}
		
		
		ICourseSort iCourseSort2 = new ICourseSort() {
			
			//实现排序逻辑,按id降序排序
			@Override
			public void sort(PbCourse[] courses) {
				for (int i = 0; i < courses.length; i++) {
					for (int j = i+1; j < courses.length; j++) {
						if(courses[i]!=null
								&& courses[j]!=null
								&& courses[i].getId()>courses[j].getId()) {
							//元素交换
							PbCourse course = courses[i];
							courses[i] = courses[j];
							courses[j] = course;
						}
					}
				}
				
			}
		};
		System.out.println("=============实现id的升序排序=============");
		iCourseSort2.sort(courses);

		//遍历查询
		for (int i = 0; i < courses.length; i++) {
			System.out.println(courses[i]);
		}
		
	}

}
